package unclassified.simple;

import java.util.HashMap;
import java.util.Map;

public class RansomNote_383 {

    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            char c = magazine.charAt(i);
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            char c = ransomNote.charAt(i);
            if (map.containsKey(c)){
                if (map.get(c) > 0) {
                    map.put(c, map.get(c) - 1);
                    continue;
                }
            }
            return false;
        }
        return true;
    }

    public boolean canConstructOpt(String ransomNote, String magazine) {
        int[] arr = new int[26];
        for (int i = 0; i < magazine.length(); i++) {
            arr[magazine.charAt(i) - 'a'] += 1;
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            int i1 = arr[ransomNote.charAt(i) - 'a'];
            if (i1 <= 0){
                return false;
            }
            arr[ransomNote.charAt(i) - 'a'] = i1 - 1;
        }
        return true;
    }
}
